﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _0912092_BaiTap01_SymbolsReport
{
    public class DemTanSoNhom2KyTu : DemTanSoNhomKyTu
    {
        protected override void KhoiTaoDanhSachNhomKyTu()
        {
            danhSachNhomKyTu = new List<NhomKyTu>();
            string[] dsWords=textInput.Split(new char[]{' '});
            int nWord = dsWords.Length;
            for (int i = 0; i < nWord; i++)
            {
                //Lay mot nhom 2 ky tu trong word.
                String curWord = dsWords[i];
                int lengthWord = curWord.Length;
                if (lengthWord >= 2)
                {
                    for (int j = 0; j < lengthWord - 1; j++)
                    {
                        String subStr = curWord.Substring(j, 2);
                        int nNhomKyTu = danhSachNhomKyTu.Count;
                        bool add = true;
                        for (int h = 0; h < nNhomKyTu; h++)
                        {
                            if (danhSachNhomKyTu[h].ChuoiKyTu.CompareTo(subStr) == 0)
                            {
                                add = false;
                                break;
                            }
                        }
                        if (add)
                        {
                            NhomKyTu nkt = new NhomKyTu();
                            nkt.ChuoiKyTu = subStr;
                            danhSachNhomKyTu.Add(nkt);
                        }
                    }
                }
                
            }
        }

        protected override void DemTanSoXuatHienCuaNhomKyTu()
        {
            int nNhomKyTu=danhSachNhomKyTu.Count;
            int lengthInput = textInput.Length;
            for(int i=0;i<nNhomKyTu;i++)
            {
                String curNKT = danhSachNhomKyTu[i].ChuoiKyTu;
                int oldIndex=0;
                int beginIndex=0;
                while (beginIndex < lengthInput)
                {
                    int index = textInput.IndexOf(curNKT, beginIndex);
                    if (index >= 0)
                    {
                        beginIndex = index + 1; ;
                        danhSachNhomKyTu[i].TanSo++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }

        protected override void HienThiTitle()
        {
            Console.WriteLine("*** TOP 10 NHOM 2 KY TU ***");
        }
    }
}
